package com.example.lkfy.utils;

import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

@Component
public  class RedisMqUtil {
    @Autowired
    private RedisTemplate redisTemplate;

    /***
     * redis队列添加主题数据
     * @param topic
     * @param obj
     */
    public void toMq(String topic, Object obj){
        String data = obj instanceof String ?(String)obj : JSON.toJSONString(obj);
        redisTemplate.opsForList().leftPush(topic,  data);
    }

    /**
     * 获取当前key的Value
     * @param key
     * @return
     */
    public Object rightPop(String key) {
        return redisTemplate.opsForList().rightPop(key);
    }

    /**
     * 右推送
     * @param key
     * @param value
     */
    public void rightPush(Object key, Object value){
        redisTemplate.opsForList().rightPush(key,value);
    }
}
